package com.example.controller;

import com.example.domain.ResponseResult;
import com.example.enums.AppHttpCodeEnum;
import com.example.handler.exception.SystemException;
import com.example.mapper.CollegeMapper;
import com.example.service.CollegeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.Map;

@RestController
@Validated
@RequestMapping("/college")
public class CollegeController {

    @Resource
    private CollegeService collegeService;

    @PostMapping ("/list")
    @PreAuthorize("@ps.hasPermission('collegeList')")
    public ResponseResult getCollegeList(){
        return collegeService.getCollegeList();
    }

    @PostMapping ("/add")
    @PreAuthorize("@ps.hasPermission('collegeAdd')")
    public ResponseResult addCollege(@RequestBody Map<String,String> map){
        String name = map.get("name");
        if (java.util.regex.Pattern.matches("[a-zA-Zu4e00-u9fa5]+",name)){
            throw new SystemException(AppHttpCodeEnum.PARAM_ERROR);
        }
        return collegeService.addCollege(name);
    }

    @PostMapping ("/delete")
    @PreAuthorize("@ps.hasPermission('collegeDelete')")
    public ResponseResult deleteCollege(@RequestBody Map<String,Integer> map){
        Integer id = map.get("id");
        return collegeService.deleteCollege(id);
    }

    @PostMapping ("/modInit")
    @PreAuthorize("@ps.hasPermission('collegeEdit')")
    public ResponseResult modInitCollege(@RequestBody Map<String,Integer> map){
        Integer id = map.get("id");
        return collegeService.modInitCollege(id);
    }

    @PostMapping ("/mod")
    @PreAuthorize("@ps.hasPermission('collegeEdit')")
    public ResponseResult modCollege(@RequestBody Map<String,Object> map){
        Integer id = (Integer) map.get("id");
        String name = (String) map.get("name");
        if (java.util.regex.Pattern.matches("[a-zA-Zu4e00-u9fa5]+",name)){
            throw new SystemException(AppHttpCodeEnum.PARAM_ERROR);
        }
        return collegeService.modCollege(id,name);
    }

}
